package main

import (
	"FlyBird/util"
	"github.com/hajimehoshi/ebiten"
	"image/png"
	"os"
)

type NewBarrier struct {
	Image  *ebiten.Image
	X      float64   // x坐标
	TopY   int  //  障碍物向上的坐标
	BottomImage *ebiten.Image
	TopLength int  //  需要重叠的次数
	BottomY int   // 障碍物向下的坐标
	UpImage *ebiten.Image
	HoverUPY int  // 悬浮障碍物的上坐标
	HoverBottomY int  //  悬浮障碍物的下坐标
	BarrierStates int  // 障碍物的形态
	isScore bool  // 是否已经被记录过分数
}
//                图片路径           图片 X 坐标 图片 Y 坐标  末尾图片的Y坐标    整个柱子的长度
func NewObstacle(imagePath string, x float64, topY int, bottomImgPath string ,TopLength int , bottomY int , upImgPath string, hoverUp int,hoverBottom int,barrierState int) (*NewBarrier, error) {
	f, _ := os.Open(imagePath)

	img, _ := png.Decode(f)

	filter := ebiten.FilterNearest
	eImg, _ := ebiten.NewImageFromImage(img, filter)

	f1, _ := os.Open(bottomImgPath)

	img1, _ := png.Decode(f1)

	filter1 := ebiten.FilterNearest
	bottomImg, _ := ebiten.NewImageFromImage(img1, filter1)

	f2, _ := os.Open(upImgPath)
	img2, _ := png.Decode(f2)
	filter2 := ebiten.FilterNearest
	upImg, _ := ebiten.NewImageFromImage(img2, filter2)

	newBarrier := &NewBarrier{
		Image:  eImg,
		X:      x,
		TopY:   topY,
		BottomImage: bottomImg,
		TopLength: TopLength,
		UpImage : upImg,
		BottomY: bottomY,
		HoverUPY: hoverUp,
		HoverBottomY: hoverBottom,
		BarrierStates: barrierState,
		isScore : false,
	}

	return newBarrier, nil
}


// 下方障碍物
func (o *NewBarrier) DrawTop(screen *ebiten.Image) {
	for i := 1 ;i<o.TopLength;i++{
		op := &ebiten.DrawImageOptions{}
		op.GeoM.Translate(float64(o.X),float64(o.TopY+(i-1)*20))
		screen.DrawImage(o.Image,op)
	}
	op1 := &ebiten.DrawImageOptions{}
	op1.GeoM.Translate(float64(o.X-2),float64(o.TopLength*20-20))
	screen.DrawImage(o.BottomImage,op1)
	//ebitenutil.DrawRect(screen , o.X, float64(o.TopY),40 , float64(o.TopLength*20)+3,color.White)
}

// 上方障碍物
func (o *NewBarrier) DrawBottom(screen *ebiten.Image) {
	for i := 1 ;i<19-o.TopLength;i++{
		op := &ebiten.DrawImageOptions{}
		op.GeoM.Translate(float64(o.X),float64(o.BottomY-(i-1)*20))
		screen.DrawImage(o.Image,op)
	}
	op1 := &ebiten.DrawImageOptions{}
	op1.GeoM.Translate(float64(o.X-2),float64(500-(19-o.TopLength)*20+20))
	screen.DrawImage(o.UpImage,op1)
	//ebitenutil.DrawRect(screen , o.X, float64((o.TopLength*20)+140),40 , float64(518-((o.TopLength*20)+140)) ,color.White)
}

// 中间障碍物
func (o *NewBarrier) DrawMid(screen *ebiten.Image) {
	//fmt.Println(o.HoverUPY,o.TopLength,o.HoverBottomY)
	for i := 1 ;i<o.TopLength;i++{
		op := &ebiten.DrawImageOptions{}
		op.GeoM.Translate(float64(o.X),float64(o.HoverUPY+i*20))
		screen.DrawImage(o.Image,op)
	}
	op1 := &ebiten.DrawImageOptions{}
	op1.GeoM.Translate(float64(o.X-2),float64(o.HoverBottomY))
	screen.DrawImage(o.BottomImage,op1)
	op2 := &ebiten.DrawImageOptions{}
	op2.GeoM.Translate(float64(o.X-2),float64(o.HoverUPY))
	screen.DrawImage(o.UpImage,op2)
	//ebitenutil.DrawRect(screen , o.X, float64(o.HoverUPY),40 , float64(o.HoverBottomY-o.HoverUPY+20),color.White)
}

//  绘制障碍物
func (g *Game) DrawBarrier(screen *ebiten.Image){
	if (gameState != 0){
		for _, o := range obstacles {

			if (o.BarrierStates ==0 || o.BarrierStates==1){
				o.DrawTop(screen)
				o.DrawBottom(screen)
			}else{
				o.DrawMid(screen)
			}
			if (o.X<200){
				if (!o.isScore){
					o.isScore = true
					Score++
				}
			}
			//  碰撞检测
			if util.IsColliding(g.LocationX, g.LocationY, 42, 35, o.X, 	float64(o.TopLength*20)+3,float64((o.TopLength*20)+140), 40, float64(o.HoverUPY), float64(o.HoverBottomY)) {
				// 发生碰撞！
				// 在这里处理碰撞的逻辑，比如游戏结束等
				g.BirdState = 0
			}

		}

	}
}


//  初始化 障碍物
func makeBarrier (){
	for i := 0; i < 7; i++ {
		ran := util.Random(15,2)
		ran1 := util.Random(180,80)
		ran2 := util.Random(3,0)
		o, err := NewObstacle("imgs/barrier.png", float64(500+180*i), 0, "imgs/barrier_down.png",ran,500,"imgs/barrier_up.png",ran1,ran1+20*ran,ran2)
		if err != nil {
			// 错误处理
		}
		obstacles = append(obstacles, o)
	}

}


// 让障碍物移动起来
func obojsaasdf(obs []*NewBarrier){
	for i:=0;i< len(obs);i++ {
		obs[i].X --
	}
}

